/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General
 * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package bmodeling;

import java.io.*;
import java.util.*;
import javax.swing.*;

/**
 * Starter class.
 * 
 * @author M. Hautle
 * @since 13.09.08
 */
public class Starter {
	/** The main class to start */
	private static final String MAIN_CLASS = "bmodeling.exe.Exe";

	/** The lib dir to use */
	private static final String LIB = "lib";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			System.out.println(System.currentTimeMillis());
			final String oooPath = addOOOToClassPath();
			if (oooPath == null)
				return;
			final Loader loader = new Loader(LIB, oooPath);
			// final Loader2 loader = new Loader2(LIB, oooPath);
			Thread.currentThread().setContextClassLoader(loader);
			loader.loadClass(MAIN_CLASS).getMethod("main", String[].class).invoke(null, (Object) new String[0]);
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "Fehler", "Fehler beim Starten:\n" + e.getMessage(), JOptionPane.ERROR_MESSAGE);
		}
	}

	/**
	 * Adds the openoffice libs to the classpath.
	 * 
	 * @return The OOO Dir or null
	 * @throws InvalidPropertiesFormatException
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	private static String addOOOToClassPath() throws InvalidPropertiesFormatException, FileNotFoundException, IOException {
		final Properties p = new Properties();
		final File f = new File("settings.cfg");
		if (f.exists())
			p.load(new BufferedInputStream(new FileInputStream(f)));
		String path = p.getProperty("oooPath", "");
		if (Loader.checkOOODir(path) || (path = setOOODir(p)) != null)
			return path;
		return null;
	}

	/**
	 * Asks the user
	 * 
	 * @param p
	 * @return The path or null
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	private static String setOOODir(Properties p) throws FileNotFoundException, IOException {
		final JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		fc.setDialogTitle("Openoffice Verzeichniss");
		File f = null;
		do {
			fc.showDialog(null, "OK");
			f = fc.getSelectedFile();
			// if the user clicks on cancel
			if (f == null)
				return null;
		} while (!Loader.checkOOODir(f.getAbsolutePath()));
		final String path = f.getPath();
		p.setProperty("oooPath", path);
		p.store(new BufferedOutputStream(new FileOutputStream("settings.cfg")), null);
		return path;
	}
}
